package com.sun.hunk.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;

/**
 *
 * @author Sunil
 */
public class CommonUtil {

    /**
     * Check whether the instance is null or not
     * @param o
     * @return true if the object is null
     */
    public static boolean isNull(Object o) {
        return !isNotNull(o);
    }

    /**
     * Check whether the instance is not null
     * @param o
     * @return - true if the object is null else return false;
     */
    public static boolean isNotNull(Object o) {
        if (o == null || o.toString().trim().isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Object is empty or not.
     * @param item Object
     * @return - true if the object is not null and not empty else return false;
     */
    public static boolean isNotEmpty(Object item) {
        return !isEmpty(item);
    }

    /**
     * Object is the NULL or empty .
     * @param item
     * @return true if the if the Object is NULL or empty else return false.
     */
    public static boolean isEmpty(Object item) {
        if (item == null || item.toString().isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Collection is the NULL or empty .
     * @param item
     * @return true if the if the Object is NULL or empty else return false.
     */
    public static boolean isEmpty(Collection coll) {
        if (coll == null || coll.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Is Collection is empty or not.
     * @param list
     * @return - true if the Collection is null or empty else return false.
     */
    public static boolean isNotEmpty(Collection list) {
        if (list == null || list.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean isEqualIgnoreCase(String str1, String str2) {
        if (isNull(str1) && isNull(str2)) {
            return true;
        } else if (isNull(str1) || isNull(str2)) {
            return false;
        } else {
            return (str1.equalsIgnoreCase(str2));
        }
    }

    /**
     * Check whether two Strings are equal or not.
     * @param str1
     * @param str2
     * @return - true if both strings are null or if they are equal.
     */
    public static boolean isEqual(String str1, String str2) {
        if (isNull(str1) && isNull(str2)) {
            return true;
        } else if (isNull(str1) || isNull(str2)) {
            return false;
        } else {
            return (str1.equals(str2));
        }
    }

    /**
     * Get Deep copy of any objects.
     * @param <T> - Object type
     * @param obj Object to be copied
     * @return - copy of object
     * @throws Exception - If Object or its content of class type are not Serializable.
     */
    public static <T extends Serializable> T getDeepCopy(T obj) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        Object deepCopy = ois.readObject();
        return (T) deepCopy;
    }

    /**
     * Get PrintStackTrace as String.
     * @param throwable
     * @return
     */
    public static String getStackTrace(Throwable throwable) {
        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        throwable.printStackTrace(printWriter);
        return writer.toString();
    }
}
